import {
  mutationsSetIndexData,
  mutationsRmIndexData
} from '@/store/mutations-types'
import { ActionsGetIndexData } from '@/store/actions-type'
import { getIndexData } from '@/api/base'
import {
  getIndexDataCache,
  setIndexDataCache,
  removeIndexDataCache
} from '@/utils/stroage/indexData'
const home = {
  state: {
    indexData: getIndexDataCache() || {}
  },
  getters: {},
  mutations: {
    [mutationsSetIndexData](state, indexdata) {
      state.indexData = indexdata
      setIndexDataCache(indexdata)
    },
    [mutationsRmIndexData](state) {
      state.indexData = {}
      removeIndexDataCache()
    }
  },
  actions: {
    async [ActionsGetIndexData]({ commit }) {
      try {
        const result = await getIndexData()
        commit(mutationsSetIndexData, result.data)
      } catch (error) {
        return Promise.reject(error)
      }
    }
  },
  namespaced: true
}

export default home
